#!/bin/bash

dep_nodes=()
sorted_g1_deps=()
unique_lx_deps=()
collect_deps_() {
  local initial_dep="$1"
  local initial_dep_ar=($initial_dep)
  local deps=""
  local lx_deps=""
  local queue=()
  for dep in "${initial_dep_ar[@]}"; do
    if [[ "$dep" =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then
      queue+=("$dep")
    else
      lx_deps="$lx_deps $dep"
    fi
  done
  local visited=()
  dep_nodes=() # fps_file|line_num|dep|reliance for topological sort
  while [ ${#queue[@]} -gt 0 ]; do
    local dep="${queue[0]}"
    queue=("${queue[@]:1}") # inspect the first element in queue
    [[ -z "$dep" || "$dep" == "-" ]] && continue
    [[ " ${visited[*]} " =~ " ${dep} " ]] && continue
    visited+=("$dep") # f100pthdfunc0v1.hh
    local fx6a1=${dep%%[0-9]*} # f
    local fx6b1=${dep:${#fx6a1}} # 100pthdfunc0v1.hh
    local fx6a2=${fx6b1%%[a-zA-Z]*} # 100
    local fx6b2=${fx6b1:${#fx6a2}} # pthdfunc0v1.hh
    local fx6a3=${fx6b2%%[0-9]*} # pthdfunc
    local fx6b3=${fx6b2:${#fx6a3}} # 0v1.hh
    local fx6a4=${fx6b3%%[v]*} # 0
    local fx6b4=${fx6b3:${#fx6a4}+1} # 1.hh
    local fx6a5=${fx6b4%%[.]*} # 1
    local dep_sector="$fx6a2"
    local dep_name="$fx6a3"
    local dep_tier="$fx6a4"
    local dep_version="$fx6a5"
    local fps_file="m${dep_sector}.fps"
    if [[ ! -f "$fps_file" ]]; then
      echo "$0: FPS file $fps_file not found for dependency $dep." >&2
      exit 1
    fi
    local dep_entry=$(grep "^${dep_sector}|${dep_name}|FCTN|${dep_tier}+|v${dep_version}|" "$fps_file")
    [[ -z "$dep_entry" ]] && continue
    local line_num=$(grep -n "^${dep_sector}|${dep_name}|FCTN|${dep_tier}+|v${dep_version}|" "$fps_file" | cut -d':' -f1)
    deps="$deps $dep"
    local dep_reliance=$(echo "$dep_entry" | cut -d'|' -f6)
    local dep_reliance_ar=($(echo "$dep_reliance" | sed 's|,| |g'))
    local reliance_list_g1=""
    local reliance_list_lx=""
    for sub_dep in "${dep_reliance_ar[@]}"; do
      if [[ "$sub_dep" =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then
        if [[ ! " ${visited[*]} " =~ " ${sub_dep} " ]]; then
          queue+=("$sub_dep")
        fi
        reliance_list_g1="$reliance_list_g1 $sub_dep"
      else
        reliance_list_lx="$reliance_list_lx $sub_dep"
        lx_deps="$lx_deps $sub_dep"
      fi
    done
    dep_nodes+=("$fps_file|$line_num|$dep|$reliance_list_g1|$reliance_list_lx")
  done
  local cycle_detected=false
  local temp_mark=()
  local perm_mark=()
  sorted_g1_deps=()
  topological_sort_() {
    local node="$1"
    if [[ " ${temp_mark[*]} " =~ " ${node} " ]]; then
      cycle_detected=true
      return
    fi
    if [[ " ${perm_mark[*]} " =~ " ${node} " ]]; then
      return
    fi
    temp_mark+=("$node")
    local node_entry=""
    for entry in "${dep_nodes[@]}"; do
      if [[ "$entry" =~ ^[^|]*\|[^|]*\|${node}\| ]]; then
        node_entry="$entry"
        break
      fi
    done
    if [[ -z "$node_entry" ]]; then
      return
    fi
    local reliance_g1=$(echo "$node_entry" | cut -d'|' -f4)
    local reliance_ar=($reliance_g1)
    for sub_dep in "${reliance_ar[@]}"; do
      topological_sort_ "$sub_dep"
    done
    temp_mark=(${temp_mark[@]/$node})
    perm_mark+=("$node")
    sorted_g1_deps+=("$node")
  }
  for dep in $deps; do
    topological_sort_ "$dep"
  done
  if [[ "$cycle_detected" == true ]]; then
    echo "$0: Dependency cycle detected." >&2
    exit 1
  fi
  # sorted_g1_deps
  local sorted_nodes=()
  for dep in "${sorted_g1_deps[@]}"; do
    for entry in "${dep_nodes[@]}"; do
      if [[ "$entry" =~ ^[^|]*\|[^|]*\|${dep}\| ]]; then
        sorted_nodes+=("$(echo "$entry" | cut -d'|' -f1,2,3)")
        break
      fi
    done
  done
  sorted_g1_deps=($(printf "%s\n" "${sorted_nodes[@]}" | sort -t'|' -k1,1 -k2,2n | cut -d'|' -f3))
  # unique_lx_deps
  unique_lx_deps=()
  local seen_lx=()
  for lx in $lx_deps; do
    if [[ "$lx" != "-" && ! " ${seen_lx[*]} " =~ " ${lx} " ]]; then
      unique_lx_deps+=("$lx")
      seen_lx+=("$lx")
    fi
  done
  echo "$0: G1 deps on ${sorted_g1_deps[*]}"
  echo "$0: LX deps on ${unique_lx_deps[*]}"
}

. ENV/a3blacklist.sh
is_blacklisted() {
  local name="$1"
  for black in "${BLACKLISTED_LIBS[@]}"; do
    if [[ "$name" == "$black" ]]; then
      return 0
    fi
  done
  return 1
}
is_recorded() {
  local name="$1"
  local record="$2"
  grep -qxF "$name" "$record"
}
copy_record() {
  local fullpath="$1"
  local fullpure="${fullpath%.so*}"
  local todir="$2"
  local record="$3"
  local filename="$(basename "$fullpath")"
  local filepure="${filename%.so*}"
  local dirname
  local dirname64
  local subfolder
  local depth=0
  local rel_todir=""
  local seq_rpath=("\$ORIGIN")
  local new_rpath=""
  filename_pcm1="${filepure}_rdict.pcm"
  filename_pcm2="${filepure#lib}.pcm"
  filename_rootmap="${filepure}.rootmap"
  fullpath_pcm1="${fullpure}_rdict.pcm"
  fullpath_pcm2="${fullpure#lib}.pcm"
  fullpath_rootmap="${fullpure}.rootmap"
  dirname64=${fullpath#*lib64/}
  dirname=${dirname64#*lib/} # root/TMVA/libCore.so
  if [[ "$dirname" == */* ]]; then
    subfolder=${dirname%/*} # root/TMVA
    depth=$(echo "$subfolder" | awk -F'/' '{print NF}')
    for ((i=0; i<depth; i++)); do
      rel_todir="../${rel_todir}"
      seq_rpath+=("\$ORIGIN/${rel_todir}") # $ORIGIN/../, $ORIGIN/../../, etc.
    done
  else
    subfolder=""
  fi
  IFS=':' eval 'new_rpath="${seq_rpath[*]}"'
  if is_recorded "${subfolder}/${filename}" "$record"; then
    return
  fi
  mkdir -p "${todir}/${subfolder}"
  local target_file=$(readlink -f "$fullpath" 2>/dev/null || echo "$fullpath")
  if [[ ! -f "$target_file" ]]; then
    echo "$0: Target file $target_file does not exist for $fullpath" >&2
    return 1
  fi
  local target_filename=$(basename "$target_file")
  cp "$target_file" "${todir}/${subfolder}/${target_filename}"
  patchelf --set-rpath "$new_rpath" --force-rpath "${todir}/${subfolder}/${target_filename}" # RPATH
  local src_dir=$(dirname "$target_file") # existing symlinks
  for src_file in "$src_dir/${filepure}.so"*; do
    [[ ! -e "$src_file" ]] && continue
    local src_filename=$(basename "$src_file")
    [[ "$src_filename" == "$target_filename" || ! -L "$src_file" ]] && continue
    local resolved_target=$(readlink -f "$src_file" 2>/dev/null || echo "")
    [[ "$resolved_target" == "$target_file" ]] && ln -sf "$target_filename" "${todir}/${subfolder}/${src_filename}"
  done
  local version="${target_filename##*.so}"
  if [[ -n "$version" ]]; then
    version="${version#.}"
    local version_parts
    IFS='.' read -r -a version_parts <<< "$version"
    local src_filename="${filepure}.so"
    for i in "${!version_parts[@]}"; do # ! for array indix i
      src_filename="${src_filename}.${version_parts[i]}"
      [[ "$src_filename" != "$target_filename" && ! -L "$src_filename" ]] && ln -sf "$target_filename" "${todir}/${subfolder}/${src_filename}"
    done
  fi
  # CERN ROOT
  [[ -f "${fullpath_pcm1}" && ! -f "${todir}/${subfolder}/${filename_pcm1}" ]] && cp "${fullpath_pcm1}" "${todir}/${subfolder}" # CERN ROOT dict
  [[ -f "${fullpath_pcm2}" && ! -f "${todir}/${subfolder}/${filename_pcm2}" ]] && cp "${fullpath_pcm2}" "${todir}/${subfolder}" # CERN ROOT dict
  [[ -f "${fullpath_rootmap}" && ! -f "${todir}/${subfolder}/${filename_rootmap}" ]] && cp "${fullpath_rootmap}" "${todir}/${subfolder}" # CERN ROOT rootmap
  echo "${subfolder}/${filename}" >> "$record"
}
copy_deps_recursive() {
  local lib="$1"
  local todir="$2"
  local record="$3"
  local queue=( "$lib" )
  local visited=()
  while [ ${#queue[@]} -gt 0 ]; do
    local current="${queue[0]}"
    queue=("${queue[@]:1}")
    [[ ! -f "$current" ]] && continue
    visited+=( "$current" )
    copy_record "$current" "$todir" "$record"
    while IFS= read -r dep; do
      [[ -z "$dep" ]] && continue
      [[ ! -f "$dep" ]] && continue
      local dep_name="$(basename "$dep")"
      is_blacklisted "$dep_name" && continue
      [[ " ${visited[*]} " =~ " $dep " ]] && continue
      queue+=( "$dep" )
    done < <(ldd "$current" | awk '{print $3}' | grep ^/)
  done
}

[[ -z ${m0reliance} || ${m0sector} =~ ^# ]] && { exit 0; }
cplar=($(echo ${e0compiler}))
stdar=($(echo ${e0standard}))
if [[ ${m0type} == "EXTN" ]]; then # exportation
  # port
  tier0="${m0tier//+}"
  fname="a${m0sector}${m0name}${tier0}${m0version}"
  fpdir="A${m0sector}/_${fname}/usr"; ffdir="${fpdir}/tmp"; fddir="${fpdir}/lib"; fadir="${fpdir}/bin"
  fedir="A${m0sector}/_${fname}/etc"
  rm -rf "$fpdir" "$fedir"
  mkdir -p "$fpdir" "$ffdir" "$fddir" "$fadir" "$fedir"
  ffcpy="${ffdir}/.so_copied"
  rm -f "$ffcpy"; touch "$ffcpy"
  mkdir -p "${fpdir}/share" "${fpdir}/share/applications" "${fpdir}/share/icons" "${fpdir}/share/icons/hicolor"
  mkdir -p "${fpdir}/share/icons/hicolor/16x16/apps" "${fpdir}/share/icons/hicolor/32x32/apps" "${fpdir}/share/icons/hicolor/64x64/apps" "${fpdir}/share/icons/hicolor/128x128/apps" "${fpdir}/share/icons/hicolor/256x256/apps" "${fpdir}/share/icons/hicolor/scalable/apps"
  ld_dirs=()
  collect_deps_ "$(echo ${m0reliance} | sed 's|,|\ |g')"
  for depl in "${sorted_g1_deps[@]}" # f100pthdfunc0v1.hh
  do
    fx6a1=${depl%%[0-9]*} # f
    fx6b1=${depl:${#fx6a1}} # 100pthdfunc0v1.hh
    fx6a2=${fx6b1%%[a-zA-Z]*} # 100
    fx6b2=${fx6b1:${#fx6a2}} # pthdfunc0v1.hh
    fx6a3=${fx6b2%%[0-9]*} # pthdfunc
    fx6b3=${fx6b2:${#fx6a3}} # 0v1.hh
    fx6a4=${fx6b3%%[v]*} # 0
    fx6b4=${fx6b3:${#fx6a4}+1} # 1.hh
    fx6a5=${fx6b4%%[.]*} # 1
    fps_file="m${fx6a2}.fps"
    if [[ ! -f "$fps_file" ]]; then
      echo "$0: FPS file $fps_file not found for dependency $depl."
      exit 1
    fi
    line_content=$(grep "^${fx6a2}|${fx6a3}|FCTN|${fx6a4}+|v${fx6a5}|" "$fps_file")
    if [[ -z "$line_content" ]]; then
      echo "$0: Dependency $depl not found in FPS file $fps_file."
      exit 1
    fi
    m1sector=$(echo "$line_content" | cut -d'|' -f1)
    m1name=$(echo "$line_content" | cut -d'|' -f2)
    m1tier=$(echo "$line_content" | cut -d'|' -f4)
    m1version=$(echo "$line_content" | cut -d'|' -f5)
    m1reliance=$(echo "$line_content" | cut -d'|' -f6)
    # load
    eval "e0features=\"${e0features}\""
    cmnd="${e0warning} ${e0optimize} ${e0features} -Ihhhh -I./"
    eval "e0shared=\"${e0shared}\""
    dmnd="${e0shared} -LLLL -L${fddir}/ -llll -Wl,-rpath=rrrr:\$ORIGIN/"
    # cate
    if [[ "$m1tier" == *"+" ]]; then # C++
      fhsty="hh"
      fcsty="cc"
      fccpl=${cplar[1]} # g++
      fcstd=${stdar[1]} # -std=c++20
    else # C
      fhsty="h"
      fcsty="c"
      fccpl=${cplar[0]} # gcc
      fcstd=${stdar[0]} # -std=gnu17
    fi
    fHSTY=$(echo "$fhsty" | tr '[:lower:]' '[:upper:]')
    tier1="${m1tier//+}"
    fname="f${m1sector}${m1name}${tier1}${m1version}"
    fNAME=$(echo "$fname" | tr '[:lower:]' '[:upper:]')
    # make
    fmdir="M${m1sector}"
    mkdir -p "$fmdir"
    touch ${fmdir}/_${fname}.${fhsty}
    sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fhsty}
    touch ${fmdir}/_${fname}.${fcsty}
    sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fcsty}
    # head
    reliance1ar=($(echo ${m1reliance} | sed 's|,|\ |g'))
    itmp=0
    htmp=""
    for fx6b0 in ${reliance1ar[*]}
    do
      if [[ ${fx6b0} == "-" ]]
      then
        break 1
      fi
      fx6a1=${fx6b0%%[0-9]*}; fx6b1=${fx6b0:${#fx6a1}} # f & 10tim2v1.h
      fx6a2=${fx6b1%%[a-zA-Z]*}; fx6b2=${fx6b1:${#fx6a2}} # 10 & tim2v1.h
      if [[ ${fx6b0} =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then # bash 3+ support regex in if with =~
        fx6a3=${fx6b2%%[0-9]*}; fx6b3=${fx6b2:${#fx6a3}} # tim & 2v1.h
        fx6a4=${fx6b3%%[v]*}; fx6b4=${fx6b3:${#fx6a4}+1} # 2 & 1.h
        fx6a5=${fx6b4%%[.]*}; fx6b5=${fx6b4:${#fx6a5}} # 1 & .h
        ((itmp += 1))
        if [[ $itmp == 1 ]]; then
          htmp+=$'\n'
        fi
        htmp+="#include \"${fx6b0}\""
        htmp+=$'\n'
        fx6c0=${fx6b0%.*}
        res=$(echo ${dmnd} | grep "l${fx6c0}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6c0}|")
        fi
        echo "$0: ${fname} depends on G1 FUN lib ${fx6c0}."
      else # linux libs
        if [[ ${fx6b0} == */* ]]; then
          fx6b1=${fx6b0%[/]*}'/'; fx6b2=${fx6b0:${#fx6b1}} # root6/v3/ & Core
        else
          fx6b1=''; fx6b2=${fx6b0} # '' & x11
        fi
        mkdir -p "${fddir}/${fx6b1}"
        [[ ! " ${ld_dirs[@]} " =~ " /${fx6b1} " ]] && ld_dirs+=("/${fx6b1}")
        if [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux III
          copy_deps_recursive "/usr/local/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
          [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/local/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/local/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/local/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/local/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
          res=$(echo ${cmnd} | grep "I/usr/local/include/${fx6b1}")
          if [[ -z ${res} ]]; then
            cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/local\/include\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "L${fddir}/${fx6b1}")
          if [[ -z ${res} ]]; then
            dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/${fx6b1}|")
            dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "l${fx6b2}")
          if [[ -z ${res} ]]
          then
            dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
          fi
          echo "$0: ${fname} depends on LX III lib ${fx6b0}."
        elif [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II
          copy_deps_recursive "/usr/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
          [[ -f "/usr/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
          res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
          if [[ -z ${res} ]]; then
            cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "L${fddir}/${fx6b1}")
          if [[ -z ${res} ]]; then
            dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/${fx6b1}|")
            dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "l${fx6b2}")
          if [[ -z ${res} ]]
          then
            dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
          fi
          echo "$0: ${fname} depends on LX II lib ${fx6b0}."
        elif [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II x64
          copy_deps_recursive "/usr/lib64/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
          [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/lib64/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/lib64/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/lib64/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
          res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
          if [[ -z ${res} ]]; then
            cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "L${fddir}/${fx6b1}")
          if [[ -z ${res} ]]; then
            dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/${fx6b1}|")
            dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "l${fx6b2}")
          if [[ -z ${res} ]]
          then
            dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
          fi
          echo "$0: ${fname} depends on LX II x64 lib ${fx6b0}."
        elif [[ -f "/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I
          copy_deps_recursive "/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
          [[ -f "/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
          res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
          if [[ -z ${res} ]]; then
            cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "L${fddir}/${fx6b1}")
          if [[ -z ${res} ]]; then
            dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/${fx6b1}|")
            dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "l${fx6b2}")
          if [[ -z ${res} ]]
          then
            dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
          fi
          echo "$0: ${fname} depends on LX I lib ${fx6b0}."
        elif [[ -f "/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I x64
          copy_deps_recursive "/lib64/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
          [[ -f "/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/lib64/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/lib64/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
          [[ -f "/lib64/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/lib64/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
          res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
          if [[ -z ${res} ]]; then
            cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "L${fddir}/${fx6b1}")
          if [[ -z ${res} ]]; then
            dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/${fx6b1}|")
            dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
          fi
          res=$(echo ${dmnd} | grep "l${fx6b2}")
          if [[ -z ${res} ]]
          then
            dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
          fi
          echo "$0: ${fname} depends on LX I x64 lib ${fx6b0}."
        fi
      fi
    done
    cat << EOF > ${ffdir}/${fname}.${fhsty}
#ifndef ${fNAME}_${fHSTY}
#define ${fNAME}_${fHSTY}
${htmp}
ssss
hhhh

#endif
EOF
    sed -i -e "/^hhhh/r ${fmdir}/_${fname}.${fhsty}" ${ffdir}/${fname}.${fhsty} # insert one file into another at marked line
    if [[ "${fhsty}" = "h" ]]; then
      sed -i "/^hhhh/a #include <stddef.h>" ${ffdir}/${fname}.${fhsty}
      sed -i "/^hhhh/a #include <stdlib.h>" ${ffdir}/${fname}.${fhsty}
    elif [[ "${fhsty}" = "hh" ]]; then
      sed -i "/^hhhh/a #include <cstddef>" ${ffdir}/${fname}.${fhsty}
      sed -i "/^hhhh/a #include <cstdlib>" ${ffdir}/${fname}.${fhsty}
    fi
    sed -i "/^hhhh/d" ${ffdir}/${fname}.${fhsty}
    if [[ "${fhsty}" = "h" ]]; then
      include_line_num=$(grep -n "^#include" ${ffdir}/${fname}.${fhsty} | tail -1 | cut -f1 -d':')
      sed -i "${include_line_num}"'a\
  \
  #ifdef __cplusplus\
  extern \"C\"\
  #endif
  ' ${ffdir}/${fname}.${fhsty}
    fi
    structs=$(grep -oP '^struct\s+\K\w+' "${fmdir}/_${fname}.${fhsty}")
    struct_declarations=""
    for struct in $structs; do
      struct_declarations+="struct ${struct};\n"
    done
    if [[ ! -z $struct_declarations ]]; then
      sed -i "/^ssss/a $struct_declarations" ${ffdir}/${fname}.${fhsty}
    fi
    sed -i "/^ssss/d" ${ffdir}/${fname}.${fhsty}
    echo "$0: ${ffdir}/${fname}.${fhsty}"
    # body
    cat << EOF > ${ffdir}/${fname}.${fcsty}
#include "${fname}.${fhsty}"

gggg
EOF
    sed -i -e "/^gggg/r ${fmdir}/_${fname}.${fcsty}" ${ffdir}/${fname}.${fcsty} # insert one file into another at marked line
    sed -i "/^gggg/d" ${ffdir}/${fname}.${fcsty}
    echo "$0: ${ffdir}/${fname}.${fcsty}"
    # comp
    res=$(echo ${cmnd} | grep "I${ffdir}")
    if [[ -z ${res} ]]; then
      cmnd=${cmnd/-Ihhhh/-I${ffdir}\/}
    else
      cmnd=${cmnd/' '-Ihhhh/}
    fi
    cmnd="${fccpl} ${fcstd} ${cmnd} -c ${ffdir}/${fname}.${fcsty} -o ${ffdir}/${fname}.o"
    dmnd=${dmnd/' '-LLLL/}
    dmnd=${dmnd/' '-llll/}
    dmnd=${dmnd//rrrr:/} # remove all
    dmnd="${fccpl} ${dmnd} -o ${fddir}/lib${fname}.so ${ffdir}/${fname}.o"
    ${cmnd} # compile
    echo "$0: ${cmnd}"
    if [[ ! -f ${ffdir}/${fname}.o ]]; then
      echo "$0: Failed compile ${ffdir}/${fname}.o"
      exit 1
    fi
    ${dmnd} # dynamic
    echo "$0: ${dmnd}"
    if [[ ! -f ${fddir}/lib${fname}.so ]]; then
      echo "$0: Failed dynamic ${fddir}/lib${fname}.so"
      exit 1
    fi
    rm ${ffdir}/${fname}.o
    echo "$0: ----------"
  done
  # load
  eval "e0features=\"${e0features}\""
  cmnd="${e0warning} ${e0optimize} ${e0features} -Ihhhh -I../tmp/ -LLLL -L${fddir}/ -Wl,--copy-dt-needed-entries -llll -Wl,-rpath=rrrr:\$ORIGIN/../lib/"
  # cate
  if [[ "$m0tier" == *"+" ]]; then # C++
    fhsty="hh"
    fcsty="cc"
    fccpl=${cplar[1]} # g++
    fcstd=${stdar[1]} # -std=c++20
  else # C
    fhsty="h"
    fcsty="c"
    fccpl=${cplar[0]} # gcc
    fcstd=${stdar[0]} # -std=gnu17
  fi
  fHSTY=$(echo "$fhsty" | tr '[:lower:]' '[:upper:]')
  tier0="${m0tier//+}"
  fname="a${m0sector}${m0name}${tier0}${m0version}"
  fNAME=$(echo "$fname" | tr '[:lower:]' '[:upper:]')
  # make
  fmdir="M${m0sector}"
  mkdir -p "$fmdir"
  touch ${fmdir}/_${fname}.${fhsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fhsty}
  touch ${fmdir}/_${fname}.${fcsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fcsty}
  # head
  reliance0ar=($(echo ${m0reliance} | sed 's|,|\ |g'))
  itmp=0
  htmp=""
  for fx6b0 in ${reliance0ar[@]}
  do
    if [[ ${fx6b0} == "-" ]]
    then
      break 1
    fi
    fx6a1=${fx6b0%%[0-9]*}; fx6b1=${fx6b0:${#fx6a1}} # f & 10tim2v1.h
    fx6a2=${fx6b1%%[a-zA-Z]*}; fx6b2=${fx6b1:${#fx6a2}} # 10 & tim2v1.h
    if [[ ${fx6b0} =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then # bash 3+ support regex in if with =~
      fx6a3=${fx6b2%%[0-9]*}; fx6b3=${fx6b2:${#fx6a3}} # tim & 2v1.h
      fx6a4=${fx6b3%%[v]*}; fx6b4=${fx6b3:${#fx6a4}+1} # 2 & 1.h
      fx6a5=${fx6b4%%[.]*}; fx6b5=${fx6b4:${#fx6a5}} # 1 & .h
      ((itmp += 1))
      if [[ $itmp == 1 ]]; then
        htmp+=$'\n'
      fi
      htmp+="#include \"../tmp/${fx6b0}\""
      htmp+=$'\n'
      fx6c0=${fx6b0%.*}
      res=$(echo ${cmnd} | grep "l${fx6c0}")
      if [[ -z ${res} ]]
      then
        cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6c0}|")
      fi
      echo "$0: ${fname} depends on G1 FUN lib ${fx6c0}."
    else # linux libs
      if [[ ${fx6b0} == */* ]]; then
        fx6b1=${fx6b0%[/]*}'/'; fx6b2=${fx6b0:${#fx6b1}} # root6/v3/ & Core
      else
        fx6b1=''; fx6b2=${fx6b0} # '' & x11
      fi
      [[ ! " ${ld_dirs[@]} " =~ " /${fx6b1} " ]] && ld_dirs+=("/${fx6b1}")
      if [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux III
        copy_deps_recursive "/usr/local/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
        [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/local/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/local/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/local/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/local/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
        res=$(echo ${cmnd} | grep "I/usr/local/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/local\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L${fddir}/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX III lib ${fx6b0}."
      elif [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II
        copy_deps_recursive "/usr/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
        [[ -f "/usr/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L${fddir}/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II lib ${fx6b0}."
      elif [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II x64
        copy_deps_recursive "/usr/lib64/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
        [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/usr/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/lib64/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/usr/lib64/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/usr/lib64/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L${fddir}/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II x64 lib ${fx6b0}."
      elif [[ -f "/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I
        copy_deps_recursive "/lib/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
        [[ -f "/lib/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/lib/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/lib/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/lib/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/lib/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/lib/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L${fddir}/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I lib ${fx6b0}."
      elif [[ -f "/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I x64
        copy_deps_recursive "/lib64/${fx6b1}lib${fx6b2}.so" "$fddir" "$ffcpy"
        [[ -f "/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" && ! -f "${fddir}/${fx6b1}lib${fx6b2}_rdict.pcm" ]] && cp "/lib64/${fx6b1}lib${fx6b2}_rdict.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/lib64/${fx6b1}${fx6b2}.pcm" && ! -f "${fddir}/${fx6b1}${fx6b2}.pcm" ]] && cp "/lib64/${fx6b1}${fx6b2}.pcm" "${fddir}/${fx6b1}" # CERN ROOT dict
        [[ -f "/lib64/${fx6b1}lib${fx6b2}.rootmap" && ! -f "${fddir}/${fx6b1}lib${fx6b2}.rootmap" ]] && cp "/lib64/${fx6b1}lib${fx6b2}.rootmap" "${fddir}/${fx6b1}" # CERN ROOT rootmap
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L${fddir}/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L${fddir}\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I x64 lib ${fx6b0}."
      fi
    fi
  done
  cat << EOF > ${fadir}/${fname}.${fhsty}
#ifndef ${fNAME}_${fHSTY}
#define ${fNAME}_${fHSTY}
${htmp}
hhhh

#endif
EOF
  sed -i -e "/^hhhh/r ${fmdir}/_${fname}.${fhsty}" ${fadir}/${fname}.${fhsty} # insert one file into another at marked line
  sed -i "/^hhhh/d" ${fadir}/${fname}.${fhsty}
  echo "$0: ${fadir}/${fname}.${fhsty}"
  # body
  cat << EOF > ${fadir}/${fname}.${fcsty}
#include "${fname}.${fhsty}"

int main(int argc, char** argv)
{

gggg

}
EOF
  sed -i -e "/^gggg/r ${fmdir}/_${fname}.${fcsty}" ${fadir}/${fname}.${fcsty} # insert one file into another at marked line
  sed -i "/^gggg/d" ${fadir}/${fname}.${fcsty}
  echo "$0: ${fadir}/${fname}.${fcsty}"
  # comp
  cmnd=${cmnd/' '-Ihhhh/}
  cmnd=${cmnd/' '-LLLL/}
  cmnd=${cmnd/' '-llll/}
  cmnd=${cmnd//rrrr:/} # remove all
  cmnd="${fccpl} ${fcstd} ${cmnd} ${fadir}/${fname}.${fcsty} -o ${fadir}/${fname}"
  ${cmnd} # compile
  echo "$0: ${cmnd}"
  if [[ ! -f ${fadir}/${fname} ]]; then
    echo "$0: Failed compile ${fadir}/${fname}"
    exit 1
  fi
  mv ${fadir}/${fname}.${fcsty} ${ffdir}/
  mv ${fadir}/${fname}.${fhsty} ${ffdir}/
  rm -rf ${ffdir}
  cp RSS/icon_${e0icon}_16.png ${fpdir}/share/icons/hicolor/16x16/apps/${fname}.png
  cp RSS/icon_${e0icon}_32.png ${fpdir}/share/icons/hicolor/32x32/apps/${fname}.png
  cp RSS/icon_${e0icon}_64.png ${fpdir}/share/icons/hicolor/64x64/apps/${fname}.png
  cp RSS/icon_${e0icon}_128.png ${fpdir}/share/icons/hicolor/128x128/apps/${fname}.png
  cp RSS/icon_${e0icon}_256.png ${fpdir}/share/icons/hicolor/256x256/apps/${fname}.png
  cat > "${fpdir}/share/applications/${fname}.desktop" << EOF
[Desktop Entry]
Name=${fNAME}
Exec=${fname}
Icon=${fname}
Comment=${m0comment}
Terminal=false
Type=Application
Categories=Utility;
EOF
  dire0=$(pwd)
  cd "${fpdir}/../"
  rm -f "${fname}.png"
  rm -f "${fname}.desktop"
  ln -s "usr/share/icons/hicolor/64x64/apps/${fname}.png" "${fname}.png"
  ln -s "usr/share/applications/${fname}.desktop" "${fname}.desktop"
  rm -f "AppRun"
  cat > "AppRun" << EOF
#!/bin/sh
set -e # exit on error
this_dir="\$(readlink -f "\$(dirname "\$0")")"
export PATH="\$this_dir"/usr/bin:"\$PATH"
ld_paths="./$(echo " ${ld_dirs[@]}" | sed 's| |:\${this_dir}/usr/lib|g')"
export LD_LIBRARY_PATH="\$ld_paths:\$LD_LIBRARY_PATH"
exec "\$this_dir"/usr/bin/${fname} "\$@"
EOF
  chmod +x "AppRun"
  cd "${dire0}"
  ARCH=x86_64 ENV/appimagetool-x86_64.AppImage --runtime-file ENV/runtime-x86_64 --verbose A${m0sector}/_${fname}/ A${m0sector}/${fNAME} # AppImage
  rm -rf "A${m0sector}/_${fname}" # ./${fNAME} --appimage-extract
  echo "$0: ----------"

elif [[ ${m0type} == "ACTN" ]]; then # action
  # load
  eval "e0features=\"${e0features}\""
  cmnd="${e0warning} ${e0optimize} ${e0features} -Ihhhh -LLLL -Wl,--copy-dt-needed-entries -llll -Wl,-rpath=rrrr"
  # cate
  if [[ "$m0tier" == *"+" ]]; then # C++
    fhsty="hh"
    fcsty="cc"
    fccpl=${cplar[1]} # g++
    fcstd=${stdar[1]} # -std=c++20
  else # C
    fhsty="h"
    fcsty="c"
    fccpl=${cplar[0]} # gcc
    fcstd=${stdar[0]} # -std=gnu17
  fi
  fHSTY=$(echo "$fhsty" | tr '[:lower:]' '[:upper:]')
  tier0="${m0tier//+}"
  fname="a${m0sector}${m0name}${tier0}${m0version}"
  fNAME=$(echo "$fname" | tr '[:lower:]' '[:upper:]')
  # make
  fadir="A${m0sector}"; ffdir="F${m0sector}"
  mkdir -p "$fadir" "$ffdir"
  fmdir="M${m0sector}"
  mkdir -p "$fmdir"
  touch ${fmdir}/_${fname}.${fhsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fhsty}
  touch ${fmdir}/_${fname}.${fcsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fcsty}
  # head
  reliance0ar=($(echo ${m0reliance} | sed 's|,|\ |g'))
  itmp=0
  htmp=""
  for fx6b0 in ${reliance0ar[@]}
  do
    if [[ ${fx6b0} == "-" ]]
    then
      break 1
    fi
    fx6a1=${fx6b0%%[0-9]*}; fx6b1=${fx6b0:${#fx6a1}} # f & 10tim2v1.h
    fx6a2=${fx6b1%%[a-zA-Z]*}; fx6b2=${fx6b1:${#fx6a2}} # 10 & tim2v1.h
    if [[ ${fx6b0} =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then # bash 3+ support regex in if with =~
      fx6a3=${fx6b2%%[0-9]*}; fx6b3=${fx6b2:${#fx6a3}} # tim & 2v1.h
      fx6a4=${fx6b3%%[v]*}; fx6b4=${fx6b3:${#fx6a4}+1} # 2 & 1.h
      fx6a5=${fx6b4%%[.]*}; fx6b5=${fx6b4:${#fx6a5}} # 1 & .h
      ((itmp += 1))
      if [[ $itmp == 1 ]]; then
        htmp+=$'\n'
      fi
      htmp+="#include \"../F${fx6a2}/${fx6b0}\""
      htmp+=$'\n'
      res=$(echo ${cmnd} | grep ":F${fx6a2}/")
      if [[ -z ${res} ]]; then
        cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -IF${fx6a2}/|") # insert one string into another marked string
        cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/D${fx6a2}\/|")
        cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -LD${fx6a2}/|")
      fi
      fx6c0=${fx6b0%.*}
      res=$(echo ${cmnd} | grep "l${fx6c0}")
      if [[ -z ${res} ]]
      then
        cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6c0}|")
      fi
      echo "$0: ${fname} depends on G1 FUN lib ${fx6c0}."
    else # linux libs
      if [[ ${fx6b0} == */* ]]; then
        fx6b1=${fx6b0%[/]*}'/'; fx6b2=${fx6b0:${#fx6b1}} # root6/v3/ & Core
      else
        fx6b1=''; fx6b2=${fx6b0} # '' & x11
      fi
      if [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux III
        res=$(echo ${cmnd} | grep "I/usr/local/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/local\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L/usr/local/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\/usr\/local\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L\/usr\/local\/lib\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX III lib ${fx6b0}."
      elif [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L/usr/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\/usr\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L\/usr\/lib\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II lib ${fx6b0}."
      elif [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II x64
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L/usr/lib64/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\/usr\/lib64\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L\/usr\/lib64\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II x64 lib ${fx6b0}."
      elif [[ -f "/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\/lib\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L\/lib\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I lib ${fx6b0}."
      elif [[ -f "/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I x64
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "L/lib64/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|rrrr|&:\/lib64\/${fx6b1}|")
          cmnd=$(echo ${cmnd} | sed "s|-LLLL|& -L\/lib64\/${fx6b1}|")
        fi
        res=$(echo ${cmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          cmnd=$(echo ${cmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I x64 lib ${fx6b0}."
      fi
    fi
  done
  cat << EOF > ${fadir}/${fname}.${fhsty}
#ifndef ${fNAME}_${fHSTY}
#define ${fNAME}_${fHSTY}
${htmp}
hhhh

#endif
EOF
  sed -i -e "/^hhhh/r ${fmdir}/_${fname}.${fhsty}" ${fadir}/${fname}.${fhsty} # insert one file into another at marked line
  sed -i "/^hhhh/d" ${fadir}/${fname}.${fhsty}
  echo "$0: ${fadir}/${fname}.${fhsty}"
  # body
  cat << EOF > ${fadir}/${fname}.${fcsty}
#include "../${fadir}/${fname}.${fhsty}"

int main(int argc, char** argv)
{

gggg

}
EOF
  sed -i -e "/^gggg/r ${fmdir}/_${fname}.${fcsty}" ${fadir}/${fname}.${fcsty} # insert one file into another at marked line
  sed -i "/^gggg/d" ${fadir}/${fname}.${fcsty}
  echo "$0: ${fadir}/${fname}.${fcsty}"
  # comp
  cmnd=${cmnd/' '-Ihhhh/}
  cmnd=${cmnd/' '-LLLL/}
  cmnd=${cmnd/' '-llll/}
  cmnd=${cmnd//rrrr:/} # remove all
  cmnd="${fccpl} ${fcstd} ${cmnd} ${fadir}/${fname}.${fcsty} -o ${fadir}/${fname}"
  ${cmnd} # compile
  echo "$0: ${cmnd}"
  if [[ ! -f ${fadir}/${fname} ]]; then
    echo "$0: Failed compile ${fadir}/${fname}"
    exit 1
  fi
  mv ${fadir}/${fname}.${fcsty} ${ffdir}/
  mv ${fadir}/${fname}.${fhsty} ${ffdir}/
  echo "$0: ----------"

else # function
  # load
  eval "e0features=\"${e0features}\""
  cmnd="${e0warning} ${e0optimize} ${e0features} -Ihhhh"
  eval "e0shared=\"${e0shared}\""
  dmnd="${e0shared} -LLLL -llll -Wl,-rpath=rrrr"
  smnd="${e0static}"
  # cate
  if [[ "$m0tier" == *"+" ]]; then # C++
    fhsty="hh"
    fcsty="cc"
    fccpl=${cplar[1]} # g++
    fcstd=${stdar[1]} # -std=c++20
  else # C
    fhsty="h"
    fcsty="c"
    fccpl=${cplar[0]} # gcc
    fcstd=${stdar[0]} # -std=gnu17
  fi
  fHSTY=$(echo "$fhsty" | tr '[:lower:]' '[:upper:]')
  tier0="${m0tier//+}"
  fname="f${m0sector}${m0name}${tier0}${m0version}"
  fNAME=$(echo "$fname" | tr '[:lower:]' '[:upper:]')
  # make
  ffdir="F${m0sector}"; fsdir="S${m0sector}"; fddir="D${m0sector}"
  mkdir -p "$ffdir"
  mkdir -p "$fsdir"
  mkdir -p "$fddir"
  fmdir="M${m0sector}"
  mkdir -p "$fmdir"
  touch ${fmdir}/_${fname}.${fhsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fhsty}
  touch ${fmdir}/_${fname}.${fcsty}
  sed -i 's/[[:space:]]\+$//' ${fmdir}/_${fname}.${fcsty}
  # head
  reliance0ar=($(echo ${m0reliance} | sed 's|,|\ |g'))
  itmp=0
  htmp=""
  for fx6b0 in ${reliance0ar[@]}
  do
    if [[ ${fx6b0} == "-" ]]
    then
      break 1
    fi
    fx6a1=${fx6b0%%[0-9]*}; fx6b1=${fx6b0:${#fx6a1}} # f & 10tim2v1.h
    fx6a2=${fx6b1%%[a-zA-Z]*}; fx6b2=${fx6b1:${#fx6a2}} # 10 & tim2v1.h
    if [[ ${fx6b0} =~ ^[f][0-9]+[a-zA-Z]+[0-9]+[v][0-9]+[.][hHpPxX]+ ]]; then # bash 3+ support regex in if with =~
      fx6a3=${fx6b2%%[0-9]*}; fx6b3=${fx6b2:${#fx6a3}} # tim & 2v1.h
      fx6a4=${fx6b3%%[v]*}; fx6b4=${fx6b3:${#fx6a4}+1} # 2 & 1.h
      fx6a5=${fx6b4%%[.]*}; fx6b5=${fx6b4:${#fx6a5}} # 1 & .h
      ((itmp += 1))
      if [[ $itmp == 1 ]]; then
        htmp+=$'\n'
      fi
      htmp+="#include \"../F${fx6a2}/${fx6b0}\""
      htmp+=$'\n'
      res=$(echo ${cmnd} | grep ":F${fx6a2}/")
      if [[ -z ${res} ]]; then
        cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -IF${fx6a2}\/|") # insert one string into another marked string
        dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -LD${fx6a2}\/|")
        dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\$ORIGIN\/..\/D${fx6a2}\/|")
      fi
      fx6c0=${fx6b0%.*}
      res=$(echo ${dmnd} | grep "l${fx6c0}")
      if [[ -z ${res} ]]
      then
        dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6c0}|")
      fi
      echo "$0: ${fname} depends on G1 FUN lib ${fx6c0}."
    else # linux libs
      if [[ ${fx6b0} == */* ]]; then
        fx6b1=${fx6b0%[/]*}'/'; fx6b2=${fx6b0:${#fx6b1}} # root6/v3/ & Core
      else
        fx6b1=''; fx6b2=${fx6b0} # '' & x11
      fi
      if [[ -f "/usr/local/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux III
        res=$(echo ${cmnd} | grep "I/usr/local/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/local\/include\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "L/usr/local/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\/usr\/local\/lib\/${fx6b1}|")
          dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L\/usr\/local\/lib\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX III lib ${fx6b0}."
      elif [[ -f "/usr/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "L/usr/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\/usr\/lib\/${fx6b1}|")
          dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L\/usr\/lib\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II lib ${fx6b0}."
      elif [[ -f "/usr/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux II x64
        res=$(echo ${cmnd} | grep "I/usr/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/usr\/include\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "L/usr/lib64/${fx6b1}")
        if [[ -z ${res} ]]; then
          dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\/usr\/lib64\/${fx6b1}|")
          dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L\/usr\/lib64\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX II x64 lib ${fx6b0}."
      elif [[ -f "/lib/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "L/lib/${fx6b1}")
        if [[ -z ${res} ]]; then
          dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\/lib\/${fx6b1}|")
          dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L\/lib\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I lib ${fx6b0}."
      elif [[ -f "/lib64/${fx6b1}lib${fx6b2}.so" ]]; then # Linux I x64
        res=$(echo ${cmnd} | grep "I/include/${fx6b1}")
        if [[ -z ${res} ]]; then
          cmnd=$(echo ${cmnd} | sed "s|-Ihhhh|& -I\/include\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "L/lib64/${fx6b1}")
        if [[ -z ${res} ]]; then
          dmnd=$(echo ${dmnd} | sed "s|rrrr|&:\/lib64\/${fx6b1}|")
          dmnd=$(echo ${dmnd} | sed "s|-LLLL|& -L\/lib64\/${fx6b1}|")
        fi
        res=$(echo ${dmnd} | grep "l${fx6b2}")
        if [[ -z ${res} ]]
        then
          dmnd=$(echo ${dmnd} | sed "s|-llll|& -l${fx6b2}|")
        fi
        echo "$0: ${fname} depends on LX I x64 lib ${fx6b0}."
      fi
    fi
  done
  cat << EOF > ${ffdir}/${fname}.${fhsty}
#ifndef ${fNAME}_${fHSTY}
#define ${fNAME}_${fHSTY}
${htmp}
ssss
hhhh

#endif
EOF
  sed -i -e "/^hhhh/r ${fmdir}/_${fname}.${fhsty}" ${ffdir}/${fname}.${fhsty} # insert one file into another at marked line
  if [[ "${fhsty}" = "h" ]]; then
    sed -i "/^hhhh/a #include <stddef.h>" ${ffdir}/${fname}.${fhsty}
    sed -i "/^hhhh/a #include <stdlib.h>" ${ffdir}/${fname}.${fhsty}
  elif [[ "${fhsty}" = "hh" ]]; then
    sed -i "/^hhhh/a #include <cstddef>" ${ffdir}/${fname}.${fhsty}
    sed -i "/^hhhh/a #include <cstdlib>" ${ffdir}/${fname}.${fhsty}
  fi
  sed -i "/^hhhh/d" ${ffdir}/${fname}.${fhsty}
  if [[ "${fhsty}" = "h" ]]; then
    include_line_num=$(grep -n "^#include" ${ffdir}/${fname}.${fhsty} | tail -1 | cut -f1 -d':')
    sed -i "${include_line_num}"'a\
\
#ifdef __cplusplus\
extern \"C\"\
#endif
' ${ffdir}/${fname}.${fhsty}
  fi
  structs=$(grep -oP '^struct\s+\K\w+' "${fmdir}/_${fname}.${fhsty}")
  struct_declarations=""
  for struct in $structs; do
    struct_declarations+="struct ${struct};\n"
  done
  if [[ ! -z $struct_declarations ]]; then
    sed -i "/^ssss/a $struct_declarations" ${ffdir}/${fname}.${fhsty}
  fi
  sed -i "/^ssss/d" ${ffdir}/${fname}.${fhsty}
  echo "$0: ${ffdir}/${fname}.${fhsty}"
  # body
  cat << EOF > ${ffdir}/${fname}.${fcsty}
#include "../${ffdir}/${fname}.${fhsty}"

gggg
EOF
  sed -i -e "/^gggg/r ${fmdir}/_${fname}.${fcsty}" ${ffdir}/${fname}.${fcsty} # insert one file into another at marked line
  sed -i "/^gggg/d" ${ffdir}/${fname}.${fcsty}
  echo "$0: ${ffdir}/${fname}.${fcsty}"
  # comp
  res=$(echo ${cmnd} | grep "I${ffdir}")
  if [[ -z ${res} ]]; then
    cmnd=${cmnd/-Ihhhh/-I${ffdir}\/}
  else
    cmnd=${cmnd/' '-Ihhhh/}
  fi
  cmnd="${fccpl} ${fcstd} ${cmnd} -c ${ffdir}/${fname}.${fcsty} -o ${ffdir}/${fname}.o"
  dmnd=${dmnd/' '-LLLL/}
  dmnd=${dmnd/' '-llll/}
  dmnd=${dmnd//rrrr:/} # remove all
  dmnd="${fccpl} ${dmnd} -o ${fddir}/lib${fname}.so ${ffdir}/${fname}.o"
  smnd="ar ${smnd} ${fsdir}/lib${fname}.a ${ffdir}/${fname}.o"
  ${cmnd} # compile
  echo "$0: ${cmnd}"
  if [[ ! -f ${ffdir}/${fname}.o ]]; then
    echo "$0: Failed compile ${ffdir}/${fname}.o"
    exit 1
  fi
  ${dmnd} # dynamic
  echo "$0: ${dmnd}"
  if [[ ! -f ${fddir}/lib${fname}.so ]]; then
    echo "$0: Failed dynamic ${fddir}/lib${fname}.so"
    exit 1
  fi
  ${smnd} # archive
  echo "$0: ${smnd}"
  if [[ ! -f ${fsdir}/lib${fname}.a ]]; then
    echo "$0: Failed archive ${fsdir}/lib${fname}.a"
    exit 1
  fi
  rm ${ffdir}/${fname}.o
  echo "$0: ----------"
fi
